package com.web.webstart.business.controller;

import com.web.webstart.base.util.FileUtil;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.ZipUtils;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.business.entity.Project;
import com.web.webstart.business.service.ProjectService;
import com.web.webstart.business.service.XaVelocityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;

import static com.web.webstart.base.util.FileUtil.dealPath;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: ModelController.java
 * @Package com.web.webstart.business.controller
 * @Description: 模型控制器
 * @date 2015年3月23日 下午1:00:00
 */
@Controller
@RequestMapping("project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private XaVelocityService xaVelocityService;

    //	private static String PROJECTS_PATH = "D://template//projects//";
    static String PROJECTS_PATH = FileUtil.getProjectsPath();

    /**
     * @param nextPage
     * @param pageSize
     * @param sortData
     * @param jsonFilter
     * @return
     * @Title: findProjectPage
     * @Description: 分页查询project
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public XaResult<Page<Project>> findProjectPage(
            @RequestParam(defaultValue = "0") Integer nextPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String sortData,
            String jsonFilter) {

        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith(jsonFilter, "search_");
        return projectService.findProjectByFilter(filterParams, pageable);
    }

    /**
     * @param modelId
     * @return
     * @Title: findModelById
     * @Description: 根据ID查找单条实体
     */
    @ResponseBody
    @RequestMapping(value = "{modelId}", method = RequestMethod.GET)
    public XaResult<Project> findProjectById(@PathVariable Long modelId) {
        return projectService.findProject(modelId);
    }

    /**
     * @param model
     * @return
     * @Title: save
     * @Description: 新增一条实体
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public XaResult<Project> save(@RequestBody Project model,
                                  HttpServletRequest request) {
        Long userId = WebUitl.getCmsLoginedUserId(request);
        model.setModifyUser(userId);
        model.setCreateUser(userId);
        return projectService.createProject(model);
    }

    /**
     * @param model
     * @return
     * @Title: updateModel
     * @Description: 修改一条实体
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public XaResult<Project> updateProject(@RequestBody Project model,
                                           HttpServletRequest request) {
        Long userId = WebUitl.getCmsLoginedUserId(request);
        return projectService.updateProject(model, userId);
    }

    /**
     * @param modelId
     * @return
     * @Title: deleteModelById
     * @Description: 删除一个实体
     */
    @ResponseBody
    @RequestMapping(value = "{modelId}", method = RequestMethod.DELETE)
    public XaResult<Project> deleteProjectById(@PathVariable Long modelId,
                                               HttpServletRequest request) {
        Long userId = WebUitl.getCmsLoginedUserId(request);
        return projectService.deleteProject(modelId, userId);
    }

    /**
     * @return
     * @Title: deleteModelById
     * @Description: 发布一个项目, 将项目的实体和属性打包成zip文件下载下来
     * //@param modelId
     */
    @ResponseBody
    @RequestMapping(value = "pub/{projectId}", method = RequestMethod.GET)
    public void pubProjectById(@PathVariable Long projectId,
                               HttpServletRequest request, HttpServletResponse response) {
        OutputStream ops = null;
        try {
            ops = response.getOutputStream();
            Map<String, String> fm = null;
            fm = xaVelocityService.publishProject(projectId);
            if (XaUtil.isEmpty(fm)) {
                return;
            }
            //删除缓存目录的数据
            FileUtil.deleteAllFilesOfDir(new File(PROJECTS_PATH));
            //
            Project prj = projectService.findProject(projectId).getObject();
            String root = PROJECTS_PATH + prj.getIdentify();
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/business/entity/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/business/controller/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/business/repository/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/business/service/impl/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/test/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/remote/vo/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/remote/controller/")));
            FileUtil.makeDir(new File(dealPath(root + "/com/web/" + prj.getIdentify().toLowerCase() + "/remote/service/impl/")));
            Set<String> fset = fm.keySet();
            for (String fn : fset) {
                wirteToFile(prj.getIdentify().toLowerCase(), root, fn,
                        fm.get(fn));
            }
            String fileName = prj.getIdentify() + ".zip";
            try {
                ZipUtils.zip(root, PROJECTS_PATH + fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            File zip = new File(PROJECTS_PATH + fileName);
            FileInputStream in = new FileInputStream(zip);
            byte[] buffer = new byte[(int) zip.length()];
            in.read(buffer);
            ops.write(buffer);
            response.setContentType("application/zip; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + fileName);
            ops.flush();
            in.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ops != null) {
                    ops.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // return projectService.deleteProject(modelId,userId);
    }

    public static void wirteToFile(String projIde, String rootpath, String fileName,
                                   String content) throws IOException {
        if (fileName.indexOf("Vo.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/remote/vo/"), fileName);
        } else if (fileName.indexOf("Api") > -1 && fileName.indexOf("Controller.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/remote/controller/"), fileName);
        } else if (fileName.indexOf("Api") > -1 && fileName.indexOf("Service.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/remote/service/"), fileName);
        } else if (fileName.indexOf("Api") > -1 && fileName.indexOf("ServiceImpl.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/remote/service/impl/"), fileName);
        } else if (fileName.indexOf("Test") > -1 && fileName.indexOf("Controller.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/test/controller/"), fileName);
        } else if (fileName.indexOf("Test") > -1 && fileName.indexOf("Service.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/test/service/"), fileName);
        } else if (fileName.indexOf("ServiceImpl.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/business/service/impl/"), fileName);
        } else if (fileName.indexOf("Service.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/business/service"), fileName);
        } else if (fileName.indexOf("Controller.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/business/controller/"), fileName);
        } else if (fileName.indexOf("Repository.java") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/business/repository/"), fileName);
        } else if (fileName.indexOf("List.jsp") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/jsp/"), fileName);
        } else if (fileName.indexOf("Edit.jsp") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/jsp/"), fileName);
        } else if (fileName.indexOf(".js") > -1) {
            FileUtil.WriteFile(content, dealPath(rootpath + "/jsp/"), fileName);
        } else {
            FileUtil.WriteFile(content, dealPath(rootpath + "/com/web/" + projIde + "/business/entity/"), fileName);
        }


    }
}
